package com.modebing.es.test;

import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

public class ESTest_doc_delete_batch {

    public static void main(String[] args) throws IOException {
        // 创建es客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200,"http"))
        );
        BulkRequest request = new BulkRequest();
        for (int i = 10; i > 1; i--) {
            DeleteRequest indexRequest = new DeleteRequest().index("user").id("100" + i);
            request.add(indexRequest);
        }

        //批量删除数据
        BulkResponse bulkItemResponses = esClient.bulk(request, RequestOptions.DEFAULT);
        System.out.println(bulkItemResponses.getTook());
        System.out.println(bulkItemResponses.getItems());
        // 关闭客户端
        esClient.close();
    }
}
